import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class IntelHistoryPage extends StatefulWidget {
  const IntelHistoryPage({Key? key}) : super(key: key);

  @override
  State<IntelHistoryPage> createState() => _IntelHistoryPageState();
}

class _IntelHistoryPageState extends State<IntelHistoryPage> {
  final controller = Get.find<IntelHistoryController>();
  final state = Get.find<IntelHistoryController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: TDNavBar(
            backgroundColor: Colors.transparent,
            title: '我报知的情报',
          ),
          body: _buildBody()),
    );
  }

  _buildBody() {
    return Obx(() => EasyRefresh(
          // 下拉样式
          header: TDRefreshHeader(),
          child: ListView.builder(
              itemCount: state.intellHistory.length, itemBuilder: itemBuilder),
          // 下拉刷新回调
          onRefresh: controller.onRefresh,
        ));
  }

  @override
  void dispose() {
    Get.delete<IntelHistoryController>();
    super.dispose();
  }

  Widget? itemBuilder(BuildContext context, int index) {
    return Container(
        margin: EdgeInsets.symmetric(
            vertical: AppTheme.iDefaultPadding / 2,
            horizontal: AppTheme.iDefaultPadding),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
        ),
        padding: EdgeInsets.all(AppTheme.iDefaultPadding),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildLeft(index),
            Gaps.hGap24,
            Expanded(child: _buildRight(index)),
          ],
        ));
  }

  Widget _buildLeft(index) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Text(
          "${state.intellHistory[index].updateTime.substring(5, 6)}}/24",
          style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
        ),
        Gaps.vGap4,
        Text(
          "${state.intellHistory[index].updateTime.substring(0, 4)}",
          style: TextStyle(fontSize: 12),
        ),
        Gaps.vGap4,
        Text(
          "17:07:07",
          style: TextStyle(fontSize: 12),
        ),
        Gaps.vGap4,
        Text(
          "# ${state.intellHistory[index].qingbaoTypeName}",
          style: TextStyle(fontSize: 12, color: Colors.redAccent),
        ),
      ],
    );
  }

  Widget _buildRight(index) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "${state.intellHistory[index].position}",
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
        ),
        Gaps.vGap8,
        state.intellHistory[index].photo.isEmpty
            ? Container()
            : Container(
                height: 100,
                child: GridView.count(
                  physics: NeverScrollableScrollPhysics(),
                  crossAxisCount: 3,
                  mainAxisSpacing: 8,
                  crossAxisSpacing: 8,
                  // 每行三个项
                  children: List.generate(
                    state.intellHistory[index].photo.length, // 生成9个项
                    (i) => ClipRRect(
                      borderRadius: BorderRadius.circular(8),
                      child: MkLoadImage(
                        ConfigStore.to.baseFileUrl +
                            state.intellHistory[index].photo[i],
                      ),
                    ),
                  ),
                ),
              ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Text(
                "${state.intellHistory[index].position}",
                style: TextStyle(fontSize: 12, color: Colors.blue),
              ),
            ),
            TDTag(
              '${state.intellHistory[index].status}',
              theme: TDTagTheme.danger,
              isOutline: true,
              size: TDTagSize.small,
            )
          ],
        )
      ],
    );
  }
}
